package com.nzf.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nzf.vote.entity.Type;
import com.nzf.vote.mapper.TypeMapper;
import com.nzf.vote.mapper.VoteMapper;
import com.nzf.vote.model.dto.TypeDto;
import com.nzf.vote.service.ITypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nzf.vote.service.IVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nzf
 * @since 2022-07-25
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    VoteMapper voteMapper;

    @Override
    public List<TypeDto> getTypeDtoList() {
        List<TypeDto>typeDtoList = new ArrayList<>();
        typeMapper.selectList(null).forEach(type -> {
            TypeDto typeDto = new TypeDto();
            typeDto.setId(type.getId());
            typeDto.setName(type.getType());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("theme_type_id",type.getId());
            Long aLong = voteMapper.selectCount(queryWrapper);
            typeDto.setValue(aLong);
            typeDtoList.add(typeDto);
        });
        return typeDtoList;
    }
}
